import { Moment } from 'moment';
import { FormSchema } from '/@/components/Form/index';
import { merchantTypes } from '/@/assets/dic';
import { getAppEnvConfig } from '/@/utils/env';
import { UploadApi } from '/@/api/article/index';
import { uploadVideoApi } from '/@/api/common/index';
import moment from 'moment';
export interface addForm {
  id?: Number;
  shopName: String;
  phone: String;
}
export interface Options {
  key: String;
  value: String;
  label: String;
}
export interface IbusinessTime {
  startWeeks: String;
  endWeeks: String;
  startTime: any;
  endTime: any;
}
export interface ChildrenTime {
  startTime: any;
  endTime: any;
}
export interface Time {
  day: string;
  children?: any;
}
export const times: Time[] = [
  {
    day: '周一',
    children: [
      {
        startTime: moment('00:00', 'HH:mm'),
        endTime: moment('00:00', 'HH:mm'),
      },
    ],
  },
  {
    day: '周二',
  },
  {
    day: '周三',
  },
  {
    day: '周四',
  },
  {
    day: '周五',
  },
  {
    day: '周六',
  },
  {
    day: '周日',
  },
];
export interface FileItem {
  uid: string;
  name?: string;
  status?: string;
  response?: string;
  url?: string;
  type?: string;
  size: number;
  originFileObj: any;
}

export interface FileInfo {
  file: FileItem;
  fileList: FileItem[];
}
export const weeksOptions: Options[] = [
  {
    key: '周一',
    value: '周一',
    label: '周一',
  },
  {
    key: '周二',
    value: '周二',
    label: '周二',
  },
  {
    key: '周三',
    value: '周三',
    label: '周三',
  },
  {
    key: '周四',
    value: '周四',
    label: '周四',
  },
  {
    key: '周五',
    value: '周五',
    label: '周五',
  },
  {
    key: '周六',
    value: '周六',
    label: '周六',
  },
  {
    key: '周日',
    value: '周日',
    label: '周日',
  },
];
export const discountList: Options[] = [
  {
    key: '1',
    value: '1',
    label: '是',
  },
  {
    key: '0',
    value: '0',
    label: '否',
  },
];
export interface Result {
  code: Number;
  data: {
    area1: String | Object | Array<[]>;
    area2: String | Object | Array<[]>;
  };
  msg: String;
}
export interface Ioptions {
  key: Number | String;
  label: String;
  value: Number | String;
}
export interface FormState {
  shopName: string;
  phone: string;
  doBusinessTime: Moment;
  type: string | number;
  address: string;
  province: string;
  area: string;
  city: string;
  discount: string | number;
  discountRate: string | number;
  qualification: string | number;
  activityDescribe: string;
  remark: string;
  imgUrlsList: string[];
  videoUrlsList: string[];
}
export const rules = {
  shopName: [{ required: true, message: '请输入店铺名称', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入商家电话', trigger: 'blur' }],
  doBusinessTime: [{ required: true, message: '请选择营业时间', trigger: 'blur' }],
  type: [{ required: true, message: '请选择商家分类', trigger: 'change' }],
  address: [{ required: true, message: '请选择详细地址', trigger: 'change' }],
  city: [{ required: true, message: '请选择所在城市', trigger: 'change' }],
  area: [{ required: true, message: '请选择所在区域', trigger: 'change' }],
  // discount: [{ required: true, message: '请选择是否有折扣', trigger: 'change' }],
  qualification: [{ required: true, message: '请选择是否为独家折扣', trigger: 'change' }],
  remark: [{ required: true, message: '请输入店铺描述', trigger: 'blur' }],
  imgUrlsList: [
    {
      required: true,
      validator: async (rule, value) => {
        if (!value) {
          /* eslint-disable-next-line */
          return Promise.reject('请上传图片');
        }
        return Promise.resolve();
      },
      trigger: 'change',
    },
  ],
};
export const schemas: FormSchema[] = [
  {
    label: '店铺名称',
    field: 'shopName',
    component: 'Input',
    rules: [
      {
        required: true,
        message: '请输入店铺名称',
      },
    ],
  },
  {
    label: '商家电话',
    field: 'phone',
    component: 'Input',
    rules: [
      {
        required: true,
        message: '请输入商家电话',
      },
    ],
  },
  {
    label: '营业时间',
    field: 'doBusinessTime',
    component: 'Select',
    slot: 'businessTime',
    rules: [
      {
        required: true,
        // message: '请选择营业时间',
        validator: async (rule, value) => {
          // const timeArr = value.split(',');
          let flag: Boolean = true;
          // timeArr.map((item: string) => {
          //   let itemArr = item.split(' ');
          //   // console.log(itemArr);
          //   itemArr.map((ele) => {
          //     let eleTime = ele.split('-');
          //     if (eleTime.length >= 2) {
          //       eleTime.map((ele: string) => {
          //         if (!ele || ele == 'undefined') {
          //           return (flag = false);
          //         }
          //       });
          //     } else {
          //       return (flag = false);
          //     }
          //   });
          // });
          // if (!flag || value.length <= 0) {
          //   /* eslint-disable-next-line */
          //   return Promise.reject('请填完整营业时间');
          // }
          const data = JSON.parse(value);
          data.map((item) => {
            if (!item.children) {
              return (flag = false);
            }
            item.children.map((item) => {
              if (
                (!item.startTime && !item.endTime) ||
                item.startTime == 'Invalid date' ||
                item.endTime == 'Invalid date'
              ) {
                return (flag = false);
              }
            });
          });
          console.log(!flag, value.length <= 0, data.length <= 0);
          if (!flag || value.length <= 0 || data.length <= 0) {
            return Promise.reject('请填完整营业时间');
          }
          return Promise.resolve();
        },
      },
    ],
  },
  // {
  //   label: '-',
  //   labelWidth: 10,
  //   field: 'shopName',
  //   component: 'Select',
  //   // colProps: { xxl: 6, xl: 7, md: 12 },
  //   // slot: 'endWeek',
  // },
  {
    label: '商家分类',
    field: 'type',
    component: 'Select',
    componentProps: {
      options: merchantTypes,
    },
    rules: [
      {
        required: true,
        message: '请选择商家分类',
        trigger: 'change',
      },
    ],
  },
  {
    label: '区域位置',
    field: 'city',
    component: 'Input',
    rules: [
      {
        required: true,
        message: '请选择区域位置',
        trigger: 'change',
      },
    ],
    slot: 'citySelect',
    colProps: { xxl: 6, xl: 7, md: 12 },
  },
  {
    label: '',
    field: 'area',
    labelWidth: 0,
    component: 'Input',
    rules: [
      {
        required: true,
        message: '请选择区域位置',
        trigger: 'change',
      },
    ],
    slot: 'areaSelect',
    colProps: { xxl: 6, xl: 7, md: 12 },
  },
  {
    label: '详细地址',
    field: 'address',
    component: 'Input',
    slot: 'areaSearchInput',
    rules: [
      {
        required: true,
        validator: async (rule, value) => {
          if (!value) {
            /* eslint-disable-next-line */
            return Promise.reject('请输入详细地址');
          }
          return Promise.resolve();
        },
        trigger: 'change',
      },
    ],
  },
  // {
  //   label: '是否有折扣',
  //   field: 'discount',
  //   component: 'Select',
  //   rules: [
  //     {
  //       required: true,
  //       message: '请选择是否有折扣',
  //       trigger: 'change',
  //     },
  //   ],
  //   // slot: 'areaSearch',
  //   componentProps: {
  //     options: discountList,
  //   },
  // },
  // {
  //   label: '折扣率',
  //   field: 'discountRate',
  //   component: 'InputNumber',
  // },
  {
    label: '是否为独家折扣',
    field: 'qualification',
    component: 'Select',
    componentProps: {
      options: discountList,
    },
    // rules: [
    //   {
    //     required: true,
    //     message: '请选择是否为独家折扣',
    //     trigger: 'change',
    //   },
    // ],
  },
  {
    label: '折扣信息',
    field: 'discountInformation',
    component: 'InputTextArea',
    componentProps: {
      // maxlength: 12,
      // showCount: true,
    },
  },
  {
    label: '限时活动描述',
    field: 'activityDescribe',
    component: 'Input',
  },
  {
    label: '店铺描述',
    field: 'remark',
    component: 'InputTextArea',
    rules: [
      {
        required: true,
        message: '请输入店铺描述',
      },
    ],
  },
  {
    label: '添加首图',
    field: 'coverUrl',
    component: 'Upload',
    slot: 'coverUrlSlot',
    rules: [
      {
        required: true,
        validator: async (rule, value) => {
          if (!value) {
            /* eslint-disable-next-line */
            return Promise.reject('请添加首图');
          }
          return Promise.resolve();
        },
        trigger: 'change',
      },
    ],
  },
  {
    label: '添加图片',
    field: 'imgUrlsList',
    component: 'Upload',
    // component: 'UploadFile',
    componentProps: {
      api: UploadApi,
      accept: ['jpg', 'jepg', 'png', 'jpeg', 'JPG', 'PNG', 'JEPG', 'JPEG'],
      maxSize: 5,
    },
  },
  {
    label: '添加视频（选填）',
    field: 'videoUrlsList',
    component: 'Upload',
    // slot: 'uploadVideo',
    componentProps: {
      api: uploadVideoApi,
      accept: ['mp4', 'MP4'],
      maxSize: 100,
    },
  },
];
